Skip to content

Do not record a usage for a function-like macro name without ( - #759

Open
JayToltTech wants to merge 2 commits into
cppcheck-opensource:masterfrom
JayToltTech:macro-usage-unexpanded
Open

JayToltTech wants to merge 2 commits into
cppcheck-opensource:masterfrom
JayToltTech:macro-usage-unexpanded

Conversation

@JayToltTech

@JayToltTech JayToltTech commented Nov 13, 2026 •

Copy link
Copy Markdown

Macro::expand() adds the use location to usageList before it checks whether a function-like macro is followed by (. So with #define F(x) x, the F in int F = 1; shows up in the macro usage list even though nothing is expanded there.

Came up in cppcheck-opensource/cppcheck#9018, where cppcheck uses the usage list to apply cppcheck-suppress-macro and that phantom usage suppressed a real zerodiv. That PR filters it on the cppcheck side for now.

This moves the usageList.emplace_back below the no-arguments check. __COUNTER__ still sees its usage before it reads the count, and builtins aren't function-like so they're unaffected.

One output change goes with it. The early return now comes before expandBuiltin, so #define __COUNTER__(x) x followed by a bare __COUNTER__; gives __COUNTER__ ; where it used to give 0 ;. I think the new output is the right one, but it isn't only the usage list that changes.

The second commit fixes the gap the review found in #if. A defined name there is left for expand() to record, which it no longer does for a bare function-like name, so #if F was a usage when F was defined after it but not before. Those names are now recorded like undefined ones.

Outside #if, cppcheck dump files will have one less <macro-usage> entry per bare function-like name.

Tests are tokenMacro16 and tokenMacro17, and macroUsageArg4 from #761 now expects no usage for INT64_C;. testrunner and run-tests.py pass on macOS arm64 after the rebase onto master, and runformat with uncrustify 0.80.1 makes no changes.

I also dropped this branch into externals/simplecpp on the cppcheck-opensource/cppcheck#9018 branch. Its testrunner fails the same two tests it fails with plain simplecpp master, TestPreprocessor::garbage and TestPreprocessor::standard, which come from simplecpp being newer than cppcheck's copy. Nothing new fails.

Copilot AI balanced review requested due to automatic review settings November 13, 2026 04:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@danmar

danmar commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

sorry but CI failed and now there are conflicts.. could you please take a look? I have just released 1.10.0 but a 1.10.1 with this fix would be possible..

expand() added the use location to the usage list before checking whether
a function-like macro is followed by (, so `int F = 1;` with F defined as
a function-like macro was reported as a usage of F although nothing is
expanded. Record the usage after that check.

macroUsageArg4 expected such a usage for `INT64_C;`; it now expects none.
@JayToltTech
JayToltTech force-pushed the macro-usage-unexpanded branch from 05a5ed3 to 7e57597 Compare November 19, 2026 17:50
@JayToltTech

Copy link
Copy Markdown
Author

Independent review by Claude Fable 5.2 (Anthropic), run at the PR author's request.

Line numbers refer to simplecpp.cpp at the PR head, 7e57597.

Verdict

The change is correct for its stated aim, with one gap: #if / #elif conditions (issue A).

How it was checked: the full testrunner suite passes. I also built the parent commit and the PR head and compared the preprocessed output and the macro usage list over about 30 inputs. The only differences were usages of bare function-like macro names disappearing, plus the one output change in issue B.

What holds:

  • Builtin macros. __COUNTER__, __LINE__ and __FILE__ are object-like, so the moved check never fires for them. __COUNTER__ values are unchanged: F; __COUNTER__ F(__COUNTER__) __COUNTER__ still gives 0 1 2.
  • usageList.back().arg (line 2412). Still refers to this usage. emplace_back at line 2404 precedes it, with only the expandBuiltin return in between; nested expansions happen later.
  • Name at the end of the input. Unchanged. With nameTokInst->next null, the code falls through to getMacroParameters, which throws "Wrong number of parameters", and preprocess returns before filling the usage list. That error for #define F(x) x\nF\n exists before this PR.
  • ( on a later line. F\n(1) still expands and records one usage.
  • Comments between the name and (. With comments removed, the call expands and records a usage. With comments kept, F /*c*/ (1) was already not expanded; it now also records no usage.
  • Nested expansion. expandToken never calls expand() for a bare function-like name in a replacement list, so the change brings the top-level and argument paths in line with it. G(F) used to record a usage of F at the location of G and no longer does. G(F)(3), and #define G F with G(4), still record F once.

Issues

A. #if / #elif drop the usage, inconsistently (lines 4620-4625)

The comment at line 4620 says "usage of defined macros is recorded when they are expanded", so defined names are deliberately not added to maybeUsedMacros. That no longer holds for a bare function-like macro name.

Input Before After
#define F(x) 1
#if F
#endif
F used at 2:5 no usage
#if F
#endif
#define F(x) 1
F used at 1:5 F used at 1:5
#define F(x) 1
#if 0
#elif F || 1
#endif
F used at 3:7 no usage

The same #if F is a usage when F is defined later, but not when it is defined earlier. #ifdef F and defined(F) still count as usages in both cases.

Two ways to resolve it:

  • add such names to maybeUsedMacros at line 4621, so #if F stays a usage; or
  • decide that #if F is not a usage, and correct the comment at line 4620.

Either way it needs a test.

B. The preprocessed output changes for one input (lines 2398-2402)

The early return now also precedes expandBuiltin, so this is not purely a usage-list change:

#define __COUNTER__(x) x
__COUNTER__;

This used to output 0 ; and now outputs __COUNTER__ ;. The new result is arguably the right one and the input is contrived, but the commit message describes only the usage list changing.

Optional suggestions

  • Redundant condition at line 2412. After the early return, functionLike() && nameTokInst->next already implies nameTokInst->next->op == '(', so that test can go.
  • Test coverage. tokenMacro16 and macroUsageArg4 cover only a bare name at the top level. These cases also changed and are cheap to add:
    • G(F): no usage of F.
    • #define H(a) a F a(5) with H(F): exactly one usage of F (was two).
    • F\n(1): still one usage.
    • #define F() 1 with F; F();: one usage.
    • the #if F cases from issue A.
  • Downstream effect. cppcheck uses the list for cppcheck-suppress-macro and writes it to the dump file as <macro-usage>, so dump consumers will see fewer entries. That may be worth a line in the PR description.

In an #if or #elif condition the usage of a defined macro is left to
expand(), which no longer records one for a function-like macro name that
is not followed by (. So `#if F` was a usage when F was defined after it,
but not when F was defined before it. Record such names like undefined
ones.
@JayToltTech

Copy link
Copy Markdown
Author

Rebased onto master, conflicts are resolved.

The CI failure was the windows-11-arm job hitting the CMake non-target-directive error, which #763 already fixed. The one real conflict was with #761. macroUsageArg4 expected a usage for INT64_C;, which is exactly the phantom usage this PR removes, so it now expects none.

There's also a second commit. The review above found that #if F stopped counting as a usage when F(x) is defined before it, and that's fixed with tokenMacro17.

The workflows are waiting on approval to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants